package com.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author yangcancna
 * @date 2020-03-09
 */
public class QueryWrapperUtils {

    public static <T> QueryWrapper<T> queryWrapper(Map<String, Object> map, String ordName, String notNullColumn) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(ordName)) {
            queryWrapper.orderByAsc(ordName);
        }
        if (StringUtils.isNotEmpty(notNullColumn)) {
            queryWrapper.isNotNull(notNullColumn);
        }
        if (null == map || map.size() == 0) {
            return queryWrapper;
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey().toUpperCase();
            String dateFormatter = "yyyy-MM-dd";
            //开始时间
            if (key.endsWith("BEGIN")) {
                String substring = key.substring(0, key.length() - 5);
                queryWrapper.apply(String.format("%s>=to_date('%s','%s')", substring, entry.getValue(), dateFormatter));
                continue;
            }
            //结束时间
            if (key.endsWith("END")) {
                String substring = key.substring(0, key.length() - 3);
                queryWrapper.apply(String.format("%s<=to_date('%s','%s')", substring, entry.getValue(), dateFormatter));
                continue;
            }
            queryWrapper.eq(key, entry.getValue());
        }
        return queryWrapper;
    }
}
